<template>
  <div>
    <div class="addUser">
      <el-button
        icon="el-icon-plus"
        plain
        size="mini"
        type="primary"
        @click="showAdd=true"
      >新增项目成员
      </el-button>
    </div>
    <el-dialog
      title="人员选择"
      :visible.sync="showAdd"
      width="80%">
      <user-tree :userId="addUserId">
        <template v-slot:operation="row">
          <el-button @click="()=>{showAddRole=true;thisUser=row.row;}">添加</el-button>
        </template>
      </user-tree>
      <el-dialog
        append-to-body
        title="选择项目角色"
        :visible.sync="showAddRole"
        width="20%">
        <el-select
          v-model="form.projectRole"
          clearable
          placeholder="角色"
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.project_role"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        <el-button @click="addUser(thisUser)">确定</el-button>
        <el-button @click="showAddRole=showAdd=false">取消</el-button>
      </el-dialog>
    </el-dialog>

    <el-table :data="userList">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="人员ID" align="center" prop="userId"/>
      <el-table-column label="用户名" align="center" prop="userName"/>
      <el-table-column label="项目角色" align="center" prop="projectRole">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_role" :value="scope.row.projectRole"/>
        </template>
      </el-table-column>
      <el-table-column label="分配日期" align="center" prop="assignmentDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.assignmentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人ID" align="center" prop="createdBy"/>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人ID" align="center" prop="updatedBy"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {addUser, listProjectUserAssignment, listByProjectId} from "@/api/projectUser/userProjectUserAssignment";
import {parseTime} from "@/utils/ruoyi";
import UserTree from "@/components/UserTree/index.vue";

export default {
  name: "UserAssignment",
  dicts: ['project_role'],
  components: {UserTree},
  props: {
    projectId: {
      type: [String, Number],
    }
  },
  data() {
    return {
      form:{},
      thisUser:null,
      addUserId:null,
      userList: [],
      showAdd: false,
      showAddRole:false,
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    parseTime,
    getUserList() {
      // 调用接口获取用户列表
      listProjectUserAssignment(this.projectId).then(response => {
        this.userList = response.rows
      })
    },
    addUser(row) {
      this.form.userId=row.userId;
      this.form.projectId=this.projectId;
      this.form.userName=row.userName;
      addUser(this.form).then(response => {
        this.$message.success('添加成功')
        this.showAdd = false
        this.showAddRole= false
        this.getUserList()
      })
    },handleUpdate(row){

    },handleDelete(row){

    }
  }
}
</script>

<style scoped lang="scss">
.addUser {
  margin: 0 0 10px 0;
}
</style>
